/*
Content: Creation of relevant and isolated events in PASS
Paper: "Immigrants Return Intentions and Labor Market Behavior when the Home Country is Unsafe"
Journal: Journal of Labor Economics
Authors: Jacopo Bassetto, Teresa Freitas-Monteiro
File version: 1.1 - May 8th, 2025
Stata version: 
*/

	
****************************************		
***** STEP 1: MONTHLY EVENTS ***********************
***************************************************************************

use  "$dataevent/terror_monthly_gtd_pass_v1.dta", clear 


	
*********************************
***** MONTHLY EVENTS ************
*********************************

* For each country in the data expand to have a panel month-year for 1970 to 2018
tab year
drop if startmy==.
keep ogeblandna
bys ogeblandna: gen unique = _n
keep if unique == 1 

expand 588 // (2018-1970+1)*12=588

* Create variable startmy for all month-year combinations from 1970 to 2019
gen counter = 1 

bys ogeblandna: gen startmy = 120	// 120 is Jan1970 in the month-year number classification
bys ogeblandna: gen spell = _n - 1 
bys ogeblandna: replace startmy = startmy + spell 
format startmy %tm		
merge 1:1 startmy ogeblandna using "$dataevent/terror_monthly_gtd_pass_v1.dta", gen(_merge_terror) keepusing(ogeblandna startmy terror nkill_terror year month)


cap drop year month
g year=year(dofm(startmy))
 g month=month(dofm(startmy))
tab year if _merge_terror==2, m
	
	
tab ogeblandna year if  year>1990
sort ogeblandna startmy year month
drop if _merge_terror==2
tab _merge_terror



cap drop *_q* *_y
* If in a given month there is no event assign a zero
foreach var in  nkill_terror terror   {
replace `var' = 0 if _merge_terror == 1 // months with no terror

}
	

*******************************************
*** RELEVANT AND ISOLATED TERROR EVENTS ***
*******************************************

* Yearly totals
br ogeblandna startmy year month terror
***** First we need to compute the means
	foreach var in  nkill_terror terror    {
		* Yearly totals
		bys year ogeblandna: egen `var'_y = total(`var') 
	}
	

	
********************************************************************************
*** CREATE DETRENDED MEASURES
********************************************************************************

* RELEVANCE: Difference with respect to the yearly average in the 3, 4 and 5 years before		
cap drop *_ya
foreach var in   nkill_terror  terror     {

* Generate yearly averages (not total)
bys year ogeblandna: egen `var'_ya = mean(`var')

* Generate difference with respect to the yearly average of the previous three years
bys ogeblandna (startmy): gen `var'_m3y = (`var' - ((`var'_ya[_n-12] + `var'_ya[_n-24] + `var'_ya[_n-36])/3))

* Generate difference with respect to the yearly average of the previous four years 
bys ogeblandna (startmy): gen `var'_m4y = (`var' - ((`var'_ya[_n-12] + `var'_ya[_n-24] + `var'_ya[_n-36]+ `var'_ya[_n-48])/4))

* Generate difference with respect to the yearly average of the previous five years
bys ogeblandna (startmy): gen `var'_m5y = (`var' - ((`var'_ya[_n-12] + `var'_ya[_n-24] + `var'_ya[_n-36]+ `var'_ya[_n-48]+ `var'_ya[_n-60])/5))
}
		
			
* There is at least 1 relevant terror event
g event_terror=terror>=1  & terror!=.

foreach event in  terror   {	
foreach var in	 m5y m4y m3y {
g	event`event'_`var'=`event'_`var'>=1 
	}
	}
	
	
drop _merge_terror
drop unique counter spell
cap drop month
g month=month(dofm(startmy))
drop if year<1987
		
compress
* ISOLATE: To use multiple events in one year, we need to isolate those events around time bands
	
br  ogeblandna year month startmy
bysort ogeblandna year: g obs=_n
cap drop months_year
bysort ogeblandna (startmy): g months_year=_n

* This accumulates the events over the past 1 month, 2 months and 3 months
cap drop x 

foreach event in  terror   {	
foreach var in	 m5y m4y m3y {
		
	rangestat (sum) event`event'_`var'_p30=event`event'_`var', interval(months_year,-1,0) by(ogeblandna)
replace event`event'_`var'_p30=. if year==1987 & month==1
	
		rangestat (sum) event`event'_`var'_p60=event`event'_`var', interval(months_year,-2,0) by(ogeblandna)
replace event`event'_`var'_p60=. if year==1987 & inrange(month,1,2)

		rangestat (sum) event`event'_`var'_p90=event`event'_`var', interval(months_year,-3,0) by(ogeblandna)
replace event`event'_`var'_p90=. if year==1987 & inrange(month,1,3)
			
}
}


* Identify "isolated" events, e.g. months were in the previous 1, 2 or 3 months there were no events
foreach event in  terror    {	
foreach var in	 m5y m4y m3y {
g keep_e`event'_`var'_p30=event`event'_`var'_p30<=1
g keep_e`event'_`var'_p60=event`event'_`var'_p60<=1
g keep_e`event'_`var'_p90=event`event'_`var'_p90<=1		
}
}

* Identify the date of these isolated events
foreach event in  terror {	
foreach var in	 m5y m4y m3y {
g date_e`event'_`var'_p30=startmy if event`event'_`var'==1 & keep_e`event'_`var'_p30==1
g date_e`event'_`var'_p60=startmy if event`event'_`var'==1 & keep_e`event'_`var'_p60==1
g date_e`event'_`var'_p90=startmy if event`event'_`var'==1 & keep_e`event'_`var'_p90==1			
}
}
format date_e*_p*0 %tm



* Allocates the date of isolated events to the pre-and post-reference bands
foreach event in  terror   {	
foreach var in	 m5y m4y m3y {
rangestat (min) relevant`event'_`var'_p30=date_e`event'_`var'_p30 if keep_e`event'_`var'_p30==1, interval(startmy, -1, 1) by(ogeblandna)
rangestat (min) relevant`event'_`var'_p60=date_e`event'_`var'_p60 if keep_e`event'_`var'_p60==1, interval(startmy, -2, 2) by(ogeblandna)
rangestat (min) relevant`event'_`var'_p90=date_e`event'_`var'_p90 if keep_e`event'_`var'_p90==1, interval(startmy, -3, 3) by(ogeblandna)
}
}
format relevant*_p*0 %tm

* Creates the distance in months from the isolated event
foreach event in  terror    {	
foreach var in	 m5y m4y m3y {
g distance`event'_`var'_p30=startmy- relevant`event'_`var'_p30 if relevant`event'_`var'_p30!=.
g distance`event'_`var'_p60=startmy- relevant`event'_`var'_p60 if relevant`event'_`var'_p60!=.
g distance`event'_`var'_p90=startmy- relevant`event'_`var'_p90 if relevant`event'_`var'_p90!=.
}
}


* Make sure that for every isolated event there is a control and treatment group within the corresponding bandwidth  (this is to be consistent with the IEB)
foreach event in  terror     {	
foreach var in	 m5y m4y m3y {
g select`event'_`var'_p30=1 if date_e`event'_`var'_p30!=. & distance`event'_`var'_p30[_n-1]==-1 & distance`event'_`var'_p30[_n+1]==1
g select`event'_`var'_p60=1 if date_e`event'_`var'_p60!=. & distance`event'_`var'_p60[_n-1]==-1 & distance`event'_`var'_p60[_n+1]==1 & distance`event'_`var'_p60[_n-2]==-2 & distance`event'_`var'_p60[_n+2]==2
g select`event'_`var'_p90=1 if date_e`event'_`var'_p90!=. & distance`event'_`var'_p90[_n-1]==-1 & distance`event'_`var'_p90[_n+1]==1 & distance`event'_`var'_p90[_n-2]==-2 & distance`event'_`var'_p90[_n+2]==2 & distance`event'_`var'_p90[_n-3]==-3 & distance`event'_`var'_p90[_n+3]==3
}
}

foreach event in  terror    {	
foreach var in	 m5y m4y m3y {
replace date_e`event'_`var'_p30=. if select`event'_`var'_p30!=1
replace date_e`event'_`var'_p60=. if select`event'_`var'_p60!=1
replace date_e`event'_`var'_p90=. if select`event'_`var'_p90!=1		
}
}

foreach event in terror    {	
foreach var in	 m5y m4y m3y {

cap drop relevant`event'_`var'_p30 relevant`event'_`var'_p60 relevant`event'_`var'_p90
rangestat (min) relevant`event'_`var'_p30=date_e`event'_`var'_p30 if keep_e`event'_`var'_p30==1, interval(startmy, -1, 1) by(ogeblandna)
rangestat (min) relevant`event'_`var'_p60=date_e`event'_`var'_p60 if keep_e`event'_`var'_p60==1, interval(startmy, -2, 2) by(ogeblandna)
rangestat (min) relevant`event'_`var'_p90=date_e`event'_`var'_p90 if keep_e`event'_`var'_p90==1, interval(startmy, -3, 3) by(ogeblandna)

}
}
format relevant*_p*0 %tm

* Create the distance in months from the isolated event
foreach event in  terror   {	
foreach var in	 m5y m4y m3y {
cap drop  distance`event'_`var'_p30 distance`event'_`var'_p60 distance`event'_`var'_p90
g distance`event'_`var'_p30=startmy- relevant`event'_`var'_p30 if relevant`event'_`var'_p30!=.
g distance`event'_`var'_p60=startmy- relevant`event'_`var'_p60 if relevant`event'_`var'_p60!=.
g distance`event'_`var'_p90=startmy- relevant`event'_`var'_p90 if relevant`event'_`var'_p90!=.
}
}



save "$globalterror/final\terror_monthlydata_pass_v1.dta", replace 

	
	